package zirao.base.manager;

import android.content.Context;
import android.graphics.Typeface;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

/**
 * Created by ${lixiaonan} on 2017/12/25.
 */
public class FontManager {
    public static final String ROOT = "fonts/", ICONFONT = ROOT + "iconfont.ttf";
    private static Typeface iconFont, textENFont;

    private static FontManager     mFontManager;

    public FontManager(){}

    /**
     * 使用前必须在Application中初始化
     * */
    public void initFont(Context context){
        iconFont = getTypeface(context, FontManager.ICONFONT);
        textENFont = Typeface.create(Typeface.SANS_SERIF, Typeface.NORMAL);
    }

    public static FontManager getInstance(){
        if (null == mFontManager){
            mFontManager = new FontManager();
        }
        return mFontManager;
    }

    public Typeface getENTypeface(){
        return textENFont;
    }

    private static Typeface getTypeface(Context context, String font){
        return Typeface.createFromAsset(context.getAssets(), font);
    }

    public void markAsIconContainer(View view){
        if(view instanceof ViewGroup){
            ViewGroup vg = (ViewGroup)view;
            for (int i = 0; i < vg.getChildCount(); i++){
                View child = vg.getChildAt(i);
                markAsIconContainer(child);
            }
        }else if(view instanceof TextView){
            ((TextView)view).setTypeface(iconFont);
        }
    }

    public void markAsENContainer(View view){
        if(view instanceof ViewGroup){
            ViewGroup vg = (ViewGroup)view;
            for (int i = 0; i < vg.getChildCount(); i++){
                View child = vg.getChildAt(i);
                markAsIconContainer(child);
            }
        }else if(view instanceof TextView){
            ((TextView)view).setTypeface(textENFont);
        }
    }
}
